package com.shijie.controller.app;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.AppUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.domain.entity.Groupons;
import com.shijie.domain.entity.UserGroupons;
import com.shijie.service.GrouponsService;

/*
 *  团购活动相关接口
 *
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/groupons")
public class AppGrouponsController {

        @Resource
        private GrouponsService grouponsService;

        @Resource
        private AppUserService appUserService;

        @GetMapping
        public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "5") Integer pageSize) {
                PageHelper.startPage(page, pageSize);
                PageInfo<Groupons> pageInfo = new PageInfo<>(grouponsService.listGroupons());
                return ResultGenerator.success(pageInfo);
        }



        @GetMapping("@user")
        public Result listForUser(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "5") Integer pageSize) {
                PageHelper.startPage(page, pageSize);
                PageInfo<UserGroupons> pageInfo = new PageInfo<>(grouponsService.listGrouponsForUser(appUserService.getCurrentUser().getId()));
                return ResultGenerator.success(pageInfo);
        }


        @PutMapping
        public Result insert(@RequestBody UserGroupons userGroupons) {
                userGroupons.setUserId(appUserService.getCurrentUser().getId());
                String msg = grouponsService.insertUserGroupons(userGroupons);
                if (StringUtils.isBlank(msg)) {
                        return ResultGenerator.success();
                } else {
                        return ResultGenerator.error(msg);
                }

        }
}

